// 处理用户注册
const db = require("../lib/db")

function enroll(userName, userPassword, descs) {
    return new Promise((resolve, reject) => {
        let sql = `insert into enroll(userName, password, headImg, createTime, \`desc\`) values ('${userName}', '${userPassword}', 'default.jpg', '${new Date().toISOString().slice(0, 19).replace('T', ' ')}', '${descs}')`
        db.querySql(sql)
        let sql1 = `select * from enroll where userName = '${userName}'`
        db.querySql(sql1).then(data => {
            if (data.length == 0) {
                resolve(404)
            } else {
                let arr = []
                data.map(element => {
                    arr.push({
                        _id: Buffer.from(String(element.id)).toString("Base64"),
                        userName: element.userName,
                        createTime: element.createTime,
                        headImg: element.headImg,
                        desc: element.desc
                    })
                })
                console.log(arr)
                resolve(arr)
            }
        }).catch(err => {
            reject(err)
        })
    })
}

module.exports = enroll